package com.yl.mybatis.binding;

import com.yl.mybatis.annotation.Select;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class MapperRegistry {

    private Map<String, MapperMethod> knownMappers = new HashMap<>();

    public Map<String, MapperMethod> getKnownMappers() {
        return knownMappers;
    }

    public void setKnownMappers(Map<String, MapperMethod> knownMappers) {
        this.knownMappers = knownMappers;
    }

    public void addMapper(Class<?> type) {
        if (type.isInterface()) {
            for (Method method : type.getMethods()) {
                for (Annotation annotation : method.getAnnotations()) {
                    if (annotation instanceof Select) {
                        MapperMethod mapperModel = new MapperMethod();
                        //----获取sql
                        String sql = ((Select) annotation).value()[0];
                        mapperModel.setSql(sql.trim());
                        //获取返回值类型
                        mapperModel.setType(method.getReturnType());
                        knownMappers.put(type.getName() + "." + method.getName(), mapperModel);
                    }
                }
            }

        }
    }
}
